/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.perforce.api.IError;
import net.sourceforge.perforce.api.internal.Error;
import net.sourceforge.perforce.api.internal.Spec;
import net.sourceforge.perforce.api.internal.SpecData;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IClientForm;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

/**
  * ClientUser object for the 'p4 client' command.
  * 
  * @version 	$Revision: 33 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Client extends AbstractPerforceCommand 
{
	// ----------------------------------------------------------------------------- Private Methods
	
	/** Stores the result of the operation. */
	private Result result;
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Call this method to create a new Client instance.
	  *
	  * @return  the new Client instance.
	  */
	public static final Client newInstance()
	{
		MultiStatus status = 
			new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
			
		return new Client(status);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Client.
	  *
	  * @param status the status object. 
	  */
	private Client(MultiStatus status)
	{
		super(status);
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see AbstractPerforceCommand#outputStat(Map) */
	public void outputStat(Map strDict) 
	{
		if (strDict.containsKey("specdef") && strDict.containsKey("data")) 
		{
			Error error = new Error();
			
			result = new Result();
			result.getChangeSpecDefinition().decode((String) strDict.get("specdef"), error); 
			result.getChangeSpecDefinition().parse(
				(String) strDict.get("data"), result.getChangeSpecData(), error, 0);
			
			if (error.test())
			{
				getMultiStatus().merge(new Status(IStatus.ERROR, PerforceCorePlugin.ID, 
					error.getGeneric(), error.format(IError.EF_PLAIN), null));
			}

			error.dispose();
		}
	}

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand() 
	{
		return "client";                                                              //$NON-NLS-1$
	}
	
	// ------------------------------------------------------------------------------ Public Methods

	/** Returns the client form.
	  *
	  * @return the client form. 
	  */
	public IClientForm getResult()
	{
		return result;
	}

	// ----------------------------------------------------------------------- Public Nested Classes
	
	/** Class representing the result of the operation. */
	public static class Result implements IClientForm
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the changelist specificaiton. */
		private Spec spec = new Spec();
		/** Stores the data of the specification. */
		private SpecData specData = new SpecData();

		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IClientForm#getAccess() */
		public String getAccess()
		{
			return specData.get("Access");
		}
		
		/** @see IClientForm#getClient() */
		public String getClient()
		{
			return specData.get("Client");
		}
		
		/** @see IClientForm#getDescription() */
		public String getDescription()
		{
			return specData.get("Description");
		}
		
		/** @see IClientForm#getHost() */
		public String getHost() 
		{
			return specData.get("Host");
		}
		
		/** @see IClientForm#getLineEnd() */
		public String getLineEnd()
		{
			return specData.get("LineEnd");
		}
		
		/** @see IClientForm#getOptions() */
		public String getOptions()
		{
			return specData.get("Options");
		}
		
		/** @see IClientForm#getOwner() */
		public String getOwner()
		{
			return specData.get("Owner");
		}
		
		/** @see IClientForm#getRoot() */
		public String getRoot()
		{
			return specData.get("Root");
		}
		
		/** @see IClientForm#getUpdate() */
		public String getUpdate()
		{
			return specData.get("Update");
		}
		
		/** @see IClientForm#getView() */
		public String[] getView()
		{
			List files = new ArrayList();
	
			int i = 0;
			boolean hasMore = true;
			do
			{
				String filename = specData.get("View" + i++);
				if ("".equals(filename))
				{
					hasMore = false;
				}
				else
				{
					files.add(filename);
				}
			}
			while (hasMore);
			
			return (String[]) files.toArray(new String[files.size()]);
		}
		
		// --------------------------------------------------------------------------------- Methods
		
		/** @see IChangeResult#getChangeSpecData() */
		SpecData getChangeSpecData() 
		{ 
			return specData; 
		}

		/** @see IChangeResult#getChangeSpecDefinition() */
		Spec getChangeSpecDefinition() 
		{ 
			return spec; 
		}
	}
}
